<template>
    <section>
        <div class="section-content clearfix">
        <el-col :span="24" style="padding-bottom: 0px;">
                <el-form @submit.native.prevent :inline="true" >
                    <el-form-item >
                        <el-input v-model="filters.name" placeholder="名称" ></el-input>
                    </el-form-item>
                    <el-form-item >
                        <el-input v-model="filters.cmdType" placeholder="简码" ></el-input>
                    </el-form-item>
                    <!-- <el-form-item >
                        <el-input v-model="filters.register" placeholder="数据点" ></el-input>
                    </el-form-item> -->
                    <el-form-item>
                        <el-button type="primary" @click="getUsers">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col> 
            <!--列表-->
            <el-table :data="query"  highlight-current-row v-loading="listLoading" style="width: 100%;">
                <!-- <el-table-column type="selection"> -->
                <!-- </el-table-column> -->
                <el-table-column type="index" label="序号"  width="80" :index="indexMethod">
                </el-table-column>
                <el-table-column prop="name" label="名称" >
                </el-table-column>
                <el-table-column prop="cmdType" label="简码" >
                </el-table-column>
                <el-table-column prop="remark" label="备注" >
                </el-table-column>
                <el-table-column prop="updateTime" label="时间" >
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                            <i @click="handleEdit(scope.$index, scope.row)" title="编辑" icon="el-icon-edit" circle class="pb-icon icon-edit"></i>
                            <i @click="handleDel(scope.$index, scope.row)"  icon="el-icon-delete" circle title="删除" class="pb-icon icon-delete"></i>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 工具条 -->
            <el-col :span="24" class="toolbar">
                <el-pagination style="float:right;" layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total">
                </el-pagination>
            </el-col>
    </div>
    <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>      
                    <el-form-item label="简码" prop="cmdType">
                        <el-input v-model="addForm.cmdType" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input type="textarea" v-model="addForm.remark"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" v-model="editFormVisible" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="简码" prop="cmdType">
                        <el-input v-model="editForm.cmdType" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input type="textarea" v-model="editForm.remark"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {
    register_query,
    register_post,
    register_put,
    register_get,
    register_delete,
} from '../../api/api'
export default {
    data(){
        return{
            listLoading: false,
            addLoading: false,
            page:1,
            total: 0,
            addFormVisible: false,
            editFormVisible: false,
            query:[],
            filters:{
                name:"",
                cmdType:""
            },
            addForm:{
                name:"",
                cmdType:"",
                remark:""
            },
            editForm:{
                name:"",
                cmdType:"",
                remark:""
            },
            addFormRules:{
                name: [{
                        required: true,
                        message: '请输入名字',
                        trigger: 'blur'
                    },  { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                cmdType:[{
                        required: true,
                        message: '请输入简码',
                        trigger: 'blur'
                    }],
            },
            editFormRules:{
                name: [{
                        required: true,
                        message: '请输入名字',
                        trigger: 'blur'
                    },  { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                cmdType:[{
                        required: true,
                        message: '请输入简码',
                        trigger: 'blur'
                    }],
            },
        }
    },
    methods:{
        indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1; 
                return num;
            },
        getUsers(){
            let para = {    
                pageNo:this.page,
                pageSize:10,
                name: this.filters.name,
                cmdType: this.filters.cmdType,
            }
            register_query(para).then((res) => {
               console.log(res)
               if(res.code == 0){
                    this.query = res.data.list
                    this.total = res.data.totalCount;
               }
            })
        },
        //显示新增界面
        handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    cmdType:'',
                    remark: ''
                };
            },
        //新增
        addSubmit: function () {
            this.$refs.addForm.validate((valid) => {
                if (valid) {
                    this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    this.addLoading = true;
                        //NProgress.start();
                    let para = Object.assign({}, this.addForm);
                    console.log(para)
                    register_post(para).then((res) => {
                                this.begCode(res.code,res.message);
                            this.addLoading = false;
                            //NProgress.done();
                            
                    if (res.code == 0) {
                            this.$message({
                                message: '提交成功',
                                type: 'success'
                            });
                            this.$refs['addForm'].resetFields();
                            this.addFormVisible = false;
                            this.getUsers();
                        }
                        });
                        
                    });
                }
            });
        },
        //显示编辑界面
            handleEdit: function (index, row) {
                console.log(row)
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
        //编辑
        editSubmit: function () {
            this.$refs.editForm.validate((valid) => {
                if (valid) {
                    this.$confirm('确认提交吗？', '提示', {}).then(() => {
                        this.editLoading = true;
                        let para = Object.assign({}, this.editForm);
                        // console.log(para)
                        register_put(para).then((res) => {
                            this.begCode(res.code,res.message);
                            this.editLoading = false;
                            if (res.code == 0) {
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getUsers();
                            }
                        });
                    });
                }
            });
        },
        //删除
        handleDel: function(index, row){
        this.$confirm('确认删除吗?', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                //NProgress.start();
                let para = {
                    id: row.id
                };
                register_delete(para).then((res) => {
                        this.begCode(res.code,res.message);
                    if (res.code == 0) {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    }
                });
            }).catch(() => {

            });
        },
        handleCurrentChange(val){
                this.page = val;
                this.getUsers();
            },
    },
    mounted(){
        this.getUsers()
    }
}
</script>